package com.itheima.advice;
import com.itheima.service.LogService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class TransderAdvice {
    @Autowired
    private LogService logService;
    @Pointcut("execution(* com.itheima.service.AccountService.transfer(..))")
    public void pt(){
    }
    @Around("pt()")
    public Object method(ProceedingJoinPoint pjp){
        Object proceed = null;
        String out = pjp.getArgs()[0].toString();
        String in = pjp.getArgs()[1].toString();
        Double money = new Double(pjp.getArgs()[2].toString());
        System.out.println("----1-----");
        try {
            proceed = pjp.proceed(pjp.getArgs());
            String code = "转账成功";
            logService.log(out,in,money,code);
            System.out.println("----2-----");
        } catch (Throwable throwable) {
            System.out.println(throwable.getMessage());
            String code = "转账失败"+";原因:"+throwable.getMessage();
            logService.log(out,in,money,code);
        }
        return proceed;
    }
}
